package com.carefree.system.modules.admin.controller;

import com.carefree.common.core.Result;
import com.carefree.common.emus.BusinessTypeEnum;
import com.carefree.common.emus.OperatorTypeEnum;
import com.carefree.common.emus.StatusCodeEnum;
import com.carefree.system.common.annotation.SystemLog;
import com.carefree.system.modules.admin.service.UserService;
import com.carefree.system.modules.admin.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName:LoginController
 * @Description: 登录
 * @Author Tengbin tengbin234@163.com
 */
@Api(tags = "系统登录")
@RestController
public class LoginController {

    @Resource
    private UserService userService;

    /**
     * 登录
     *
     * @param loginVo
     * @return
     */
    @SystemLog(value = "登录", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "登录")
    @PostMapping("/sys/login")
    public Result<Object> accountLogin(@RequestBody LoginVo loginVo) {
        if (Objects.isNull(loginVo)) {
            return Result.error(StatusCodeEnum.ILLEGAL_REQUEST_ERROR.getCode(), StatusCodeEnum.ILLEGAL_REQUEST_ERROR.getMessage());
        }
        return userService.accountLogin(loginVo);
    }


    /**
     * 账号信息
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "账号信息")
    @GetMapping("/sys/info")
    public Result<Object> info() {
        Map<String, Object> resultMap = userService.getUserInfo();
        return Result.success(resultMap);
    }

    /**
     * 退出
     */
    @SystemLog(value = "退出登录", businessType = BusinessTypeEnum.OTHER, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "退出登录")
    @PostMapping("/sys/logout")
    public Result<Object> logout() {
        return userService.logout();
    }

}
